// pages/publish/publish.js
Page({
  data: {
    categories: ['房屋租赁', '二手交易', '求职招聘', '生活服务', '其他'],
    categoryIndex: -1,
    images: []
  },
  
  onCategoryChange(e) {
    this.setData({ categoryIndex: e.detail.value })
  },
  
  chooseImage() {
    wx.chooseImage({
      count: 3 - this.data.images.length,
      sizeType: ['compressed'],
      success: res => {
        this.setData({
          images: [...this.data.images, ...res.tempFilePaths]
        })
      }
    })
  },
  
  removeImage(e) {
    const index = e.currentTarget.dataset.index
    const images = [...this.data.images]
    images.splice(index, 1)
    this.setData({ images })
  },
  
  onSubmit(e) {
    const { title, description } = e.detail.value
    const category = this.data.categoryIndex >= 0 
      ? this.data.categories[this.data.categoryIndex] 
      : ''
    
    if (!title) {
      wx.showToast({
        title: '请填写标题',
        icon: 'none'
      })
      return
    }
    
    if (!category) {
      wx.showToast({
        title: '请选择分类',
        icon: 'none'
      })
      return
    }
    
    // 模拟发布请求
    wx.showLoading({ title: '发布中...' })
    setTimeout(() => {
      wx.hideLoading()
      wx.showToast({
        title: '发布成功',
        icon: 'success'
      })
      
      // 重置表单
      this.setData({
        categoryIndex: -1,
        images: []
      })
      
      // 跳转到首页
      wx.switchTab({
        url: '/pages/index/index'
      })
    }, 1500)
  }
})